#include<iostream>
#include<stack>
using namespace std;
int a[100010];
long long sum[100010];
int r[100010];
int l[100010];
int main()
{
	int n = 0;
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	stack<int> st1;
	st1.push(1);
	for (int i = 2;i <= n + 1;i++)
	{
		if (a[i] >= a[st1.top()]) st1.push(i);
		else
		{
			while (!st1.empty() && a[i] < a[st1.top()])
			{
				r[st1.top()] = i;
				st1.pop();
			}
			st1.push(i);
		}
	}
	stack<int> st2;
	st2.push(n);
	for (int i = n-1;i >=0;i--)
	{
		if (a[i] >= a[st2.top()]) st2.push(i);
		else
		{
			while (!st2.empty() && a[i] < a[st2.top()])
			{
				l[st2.top()] = i;
				st2.pop();
			}
			st2.push(i);
		}
	}
	long long max = 0;
	for (int i = 1;i <= n;i++)
	{
		long long ret = a[i] * (sum[r[i] - 1] - sum[l[i]]);
		if (ret > max) max = ret;
	}
	cout << max << endl;
	return 0;
}


